package ru.ibrae.cr.domain.utils;

import ru.ibrae.cr.client.Lookup;
import ru.ibrae.cr.domain.BaseDocument;
import ru.ibrae.cr.domain.DocumentDocumentLink;
import ru.ibrae.cr.domain.DocumentDocumentType;
import ru.ibrae.cr.domain.DocumentPersonLink;
import ru.ibrae.cr.domain.DocumentPersonType;
import ru.ibrae.cr.domain.Person;
import ru.ibrae.cr.domain.PredefinedDocumentType;

public class DocumentCreator {
    //~ Instance fields --------------------------------------------------------

    private Person person;
    private String series;
    private String mark;
    private String number;
    private BaseDocument document;
    private boolean readOnly;
    private DocumentPersonLink link;

    //~ Methods ----------------------------------------------------------------
    /**
     * person should be saved in database before be passed into this method
     *
     * @param person
     * @return
     */
    public static DocumentPersonLink createBaseDocument(final Person person,
            final String series,
            final String mark,
            final String number,
            final PredefinedDocumentType doctype,
            final DocumentPersonType linktype) {
        DocumentCreator dc = new DocumentCreator();
        dc.person = person;
        dc.series = series;
        dc.mark = mark;
        dc.number = number;

        DocumentPersonLink link = dc.getLink();

        if (( link != null ) && ( ( link.getId() != null ) || ( person == null ) )) {
            return link;
        }

        return null;
    }

    public static DocumentPersonLink createBaseDocument(final Person person,
            final PredefinedDocumentType doctype,
            final DocumentPersonType linktype) {
        return createBaseDocument(person, null, null, null, doctype, linktype);
    }

    public static DocumentPersonLink createBaseDocument(final Person person) {
        return createBaseDocument(person, null, null);
    }

    public static BaseDocument createCertificate(final String series,
            final String mark,
            final String number,
            final Person person) {
        PredefinedDocumentType certType = PredefinedDocumentType.getCertificateType(
                series);
        DocumentPersonLink link = createBaseDocument(person,
                series,
                mark,
                number,
                certType,
                DocumentPersonType.Belongs);

        if (link != null) {
            return link.getDocument();
        }

        return null;
    }

    public static BaseDocument createCertificate(final String series,
            final String mark,
            final String number,
            final Person person,
            BaseDocument request) {
        BaseDocument cert = createCertificate(series, mark, number,
                person);
        DocumentDocumentLink ddl = new DocumentDocumentLink();
        ddl.setDocument1(request);
        ddl.setDocument2(cert);
        ddl.setType(DocumentDocumentType.IncludedInto);

        DocumentDocumentLink saveObject = Lookup.getDataProvider().saveObject(ddl);

        return saveObject.getDocument2();
    }

    public DocumentPersonLink getLink() {
        return link;
    }
}
